package com.esri.android.map.ogc;

import android.util.Log;
import com.esri.android.map.TiledServiceLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.AngularUnit;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.LinearUnit;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.geometry.Unit;
import com.esri.core.internal.tasks.e.b.d;
import com.esri.core.internal.tasks.e.b.e;
import com.esri.core.internal.util.g;
import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import com.esri.core.ogc.ResourceUrlInfo;
import com.esri.core.ogc.wmts.WMTSLayerInfo;
import com.esri.core.ogc.wmts.WMTSServiceInfo;
import com.esri.core.ogc.wmts.WMTSServiceMode;
import com.esri.core.ogc.wmts.WMTSStyle;
import com.esri.core.ogc.wmts.WMTSTileMatrix;
import com.esri.core.ogc.wmts.WMTSTileMatrixLimits;
import com.esri.core.ogc.wmts.WMTSTileMatrixSet;
import com.esri.core.ogc.wmts.WMTSTileMatrixSetLink;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;

/* loaded from: classes.dex */
public class WMTSLayer extends TiledServiceLayer {
    private static final double c = 39.37d;
    private static final double d = 90.71446714322002d;
    private static final double e = 111319.491d;
    private Envelope A;
    private String B;
    private boolean C;
    private WMTSLayerInfo f;
    private WMTSServiceInfo g;
    private WMTSTileMatrixSet s;
    private String t;
    private String u;
    private String v;
    private String w;
    private SpatialReference x;
    private WMTSServiceMode y;
    private WMTSTileMatrixSetLink z;

    public WMTSLayer(String str) {
        this(str, (UserCredentials) null, true);
    }

    public WMTSLayer(String str, UserCredentials userCredentials) {
        this(str, userCredentials, true);
    }

    public WMTSLayer(String str, UserCredentials userCredentials, boolean z) {
        super(str);
        this.y = WMTSServiceMode.REST;
        this.credentials = userCredentials;
        if (z) {
            layerInitialise();
        }
    }

    public WMTSLayer(WMTSLayerInfo wMTSLayerInfo, SpatialReference spatialReference) {
        super(false);
        this.y = WMTSServiceMode.REST;
        if (wMTSLayerInfo == null) {
            throw new IllegalArgumentException("Parameter layerInfo must not be null");
        }
        this.f = wMTSLayerInfo;
        this.x = spatialReference;
        setUrl(wMTSLayerInfo.getServiceInfo().getServiceUrl());
    }

    public WMTSLayer(WMTSLayerInfo wMTSLayerInfo, SpatialReference spatialReference, String str) {
        super(true);
        this.y = WMTSServiceMode.REST;
        if (wMTSLayerInfo == null) {
            throw new IllegalArgumentException("Parameter layerInfo must not be null");
        }
        if (spatialReference == null) {
            throw new IllegalArgumentException("Parameter spatialReference must not be null");
        }
        this.f = wMTSLayerInfo;
        this.x = spatialReference;
        this.B = str;
        this.C = true;
    }

    public void layerInitialise() {
        try {
            getServiceExecutor().submit(new Runnable() { // from class: com.esri.android.map.ogc.WMTSLayer.1
                @Override // java.lang.Runnable
                public void run() {
                    WMTSLayer.this.initLayer();
                }
            });
        } catch (RejectedExecutionException unused) {
            changeStatus(OnStatusChangedListener.STATUS.fromInt(-1000));
        }
    }

    public WMTSServiceInfo getWmtsServiceInfo() {
        return this.g;
    }

    @Override // com.esri.android.map.Layer
    public String getName() {
        String name = super.getName();
        if (g.a(name)) {
            try {
                WMTSLayerInfo c2 = c();
                if (c2 != null) {
                    name = c2.getTitle();
                    setName(name);
                    return name;
                }
                return name;
            } catch (Exception unused) {
                return name;
            }
        }
        return name;
    }

    @Override // com.esri.android.map.Layer
    public String getTitle() {
        WMTSServiceInfo wMTSServiceInfo;
        String title = super.getTitle();
        return (!g.a(title) || (wMTSServiceInfo = this.g) == null) ? title : wMTSServiceInfo.getTitle();
    }

    public String getLayerId() {
        String str = this.t;
        if (str == null || str.isEmpty()) {
            try {
                WMTSLayerInfo c2 = c();
                if (c2 != null) {
                    this.t = c2.getIdentifier();
                }
            } catch (Exception unused) {
            }
        }
        return this.t;
    }

    public void setLayerId(String str) {
        this.t = str;
    }

    public String getStyle() {
        return this.u;
    }

    public void setStyle(String str) {
        this.u = str;
    }

    public String getFormat() {
        return this.v;
    }

    public void setFormat(String str) {
        this.v = str;
    }

    public WMTSServiceMode getServiceMode() {
        return this.y;
    }

    public void setServiceMode(WMTSServiceMode wMTSServiceMode) {
        this.y = wMTSServiceMode;
    }

    public void setTileMatrixSet(String str) {
        this.s = null;
        this.z = null;
        this.w = str;
    }

    @Override // com.esri.android.map.TiledServiceLayer
    protected byte[] getTile(int i, int i2, int i3) throws Exception {
        String identifier = this.s.getTileMatrices().get(i).getIdentifier();
        e eVar = null;
        if (a(identifier, i2, i3)) {
            int i4 = AnonymousClass2.a[this.y.ordinal()];
            if (i4 == 1) {
                eVar = new e(this.f.getIdentifier(), this.u, this.v, this.s.getIdentifier(), identifier, i3, i2);
            } else if (i4 == 2) {
                eVar = new e(this.B, this.u, this.s.getIdentifier(), identifier, i3, i2);
                eVar.a(this.C);
            }
            return new d(eVar, getUrl(), getCredentials()).execute();
        }
        return null;
    }

    /* renamed from: com.esri.android.map.ogc.WMTSLayer$2  reason: invalid class name */
    /* loaded from: classes.dex */
    static /* synthetic */ class AnonymousClass2 {
        static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[WMTSServiceMode.values().length];
            a = iArr;
            try {
                iArr[WMTSServiceMode.KVP.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                a[WMTSServiceMode.REST.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
        }
    }

    private boolean a(String str, int i, int i2) {
        WMTSTileMatrixLimits tileMatrixLimitsById = this.z.getTileMatrixLimitsById(str);
        return tileMatrixLimitsById == null || (i >= tileMatrixLimitsById.getMinTileCol() && i <= tileMatrixLimitsById.getMaxTileCol() && i2 >= tileMatrixLimitsById.getMinTileRow() && i2 <= tileMatrixLimitsById.getMaxTileRow());
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.TiledServiceLayer, com.esri.android.map.Layer
    public void initLayer() {
        if (getID() == 0) {
            this.nativeHandle = create();
        }
        if (getID() == 0) {
            changeStatus(OnStatusChangedListener.STATUS.fromInt(-1000));
            Log.e(com.esri.core.internal.a.a, "url =" + getUrl());
            return;
        }
        try {
            d();
            WMTSLayerInfo c2 = c();
            WMTSTileMatrixSet e2 = e();
            b(c2);
            a(c2);
            SpatialReference supportedCRS = e2.getSupportedCRS();
            Envelope a = a(c2, e2, supportedCRS);
            setInitialExtent(a);
            setFullExtent(a);
            setDefaultSpatialReference(supportedCRS);
            f();
            super.initLayer();
        } catch (Exception e3) {
            Log.e(com.esri.core.internal.a.a, "url =" + getUrl(), e3);
            if (e3 instanceof EsriSecurityException) {
                changeStatus(OnStatusChangedListener.STATUS.fromInt(((EsriSecurityException) e3).getCode()));
            } else {
                changeStatus(OnStatusChangedListener.STATUS.fromInt(-1000));
            }
        }
    }

    private void a(WMTSLayerInfo wMTSLayerInfo) {
        if (g.a(this.v)) {
            this.v = wMTSLayerInfo.getFormats().get(0);
        } else if (!wMTSLayerInfo.getFormats().contains(this.v)) {
            throw new IllegalStateException("Requested format " + this.v + " is unsupported");
        }
        ResourceUrlInfo resourceUrlByTypeAndFormat = this.f.getResourceUrlByTypeAndFormat("tile", this.v);
        if (resourceUrlByTypeAndFormat != null) {
            this.B = resourceUrlByTypeAndFormat.getTemplate();
        }
    }

    private void b(WMTSLayerInfo wMTSLayerInfo) {
        if (g.a(this.u)) {
            WMTSStyle defaultStyle = wMTSLayerInfo.getDefaultStyle();
            if (defaultStyle != null) {
                this.u = defaultStyle.getIdentifier();
            }
        } else if (wMTSLayerInfo.getStyleIds().contains(this.u)) {
        } else {
            throw new IllegalStateException("Requested style " + this.u + " is invalid");
        }
    }

    private Envelope a(WMTSLayerInfo wMTSLayerInfo, WMTSTileMatrixSet wMTSTileMatrixSet, SpatialReference spatialReference) {
        if (this.A == null) {
            this.A = new Envelope();
            com.esri.core.internal.tasks.e.a.g extentByWkid = wMTSLayerInfo.getExtentByWkid(wMTSTileMatrixSet.getSupportedCRS().getID());
            if (extentByWkid != null) {
                this.A = extentByWkid.a();
            } else {
                this.A = wMTSTileMatrixSet.getExtent();
            }
            Envelope envelope = this.A;
            if (envelope == null || envelope.isEmpty()) {
                WMTSTileMatrix wMTSTileMatrix = wMTSTileMatrixSet.getTileMatrices().get(0);
                double a = a(wMTSTileMatrix.getScaleDenominator(), spatialReference);
                Point topLeftCorner = wMTSTileMatrix.getTopLeftCorner();
                double x = topLeftCorner.getX();
                double y = topLeftCorner.getY();
                double tileWidth = wMTSTileMatrix.getTileWidth() * wMTSTileMatrix.getMatrixWidth();
                Double.isNaN(tileWidth);
                double d2 = x + (tileWidth * a);
                double tileHeight = wMTSTileMatrix.getTileHeight() * wMTSTileMatrix.getMatrixHeight();
                Double.isNaN(tileHeight);
                this.A = new Envelope(x, y - (tileHeight * a), d2, y);
            }
        }
        return this.A;
    }

    private WMTSLayerInfo c() {
        WMTSServiceInfo wMTSServiceInfo;
        if (this.f == null && (wMTSServiceInfo = this.g) != null) {
            String str = this.t;
            if (str != null) {
                WMTSLayerInfo layerInfo = wMTSServiceInfo.getLayerInfo(str);
                this.f = layerInfo;
                if (layerInfo == null) {
                    throw new IllegalStateException("Requested layer " + this.t + " is invalid");
                }
            } else {
                this.f = wMTSServiceInfo.getLayerInfos().get(0);
            }
        }
        WMTSLayerInfo wMTSLayerInfo = this.f;
        if (wMTSLayerInfo != null) {
            return wMTSLayerInfo;
        }
        throw new IllegalStateException("Unable to get layer information");
    }

    private WMTSServiceInfo d() throws Exception {
        if (this.g == null) {
            WMTSLayerInfo wMTSLayerInfo = this.f;
            if (wMTSLayerInfo != null) {
                this.g = wMTSLayerInfo.getServiceInfo();
            } else {
                this.g = WMTSServiceInfo.fetch(getUrl(), getCredentials(), this.y);
            }
        }
        return this.g;
    }

    private WMTSTileMatrixSet e() {
        WMTSTileMatrixSetLink wMTSTileMatrixSetLink;
        if (this.s == null) {
            WMTSLayerInfo c2 = c();
            WMTSServiceInfo wmtsServiceInfo = getWmtsServiceInfo();
            WMTSTileMatrixSetLink wMTSTileMatrixSetLink2 = null;
            if (this.x == null) {
                String str = this.w;
                if (str != null && !str.isEmpty()) {
                    wMTSTileMatrixSetLink = c2.getTileMatrixSetLinkById(this.w);
                } else {
                    wMTSTileMatrixSetLink = c2.getTileMatrixSetLinks().get(0);
                }
            } else {
                Iterator<WMTSTileMatrixSetLink> it = c2.getTileMatrixSetLinks().iterator();
                while (true) {
                    if (!it.hasNext()) {
                        break;
                    }
                    WMTSTileMatrixSetLink next = it.next();
                    WMTSTileMatrixSet tileMatrixSet = wmtsServiceInfo.getTileMatrixSet(next.getTileMatrixSetIdentifier());
                    if (tileMatrixSet != null && this.x.equals(tileMatrixSet.getSupportedCRS())) {
                        wMTSTileMatrixSetLink2 = next;
                        break;
                    }
                }
                if (wMTSTileMatrixSetLink2 == null) {
                    throw new IllegalStateException("Given spatial reference: " + this.x.getID() + ", is not supported");
                }
                wMTSTileMatrixSetLink = wMTSTileMatrixSetLink2;
            }
            if (wMTSTileMatrixSetLink != null) {
                WMTSTileMatrixSet tileMatrixSet2 = wmtsServiceInfo.getTileMatrixSet(wMTSTileMatrixSetLink.getTileMatrixSetIdentifier());
                this.s = tileMatrixSet2;
                a(tileMatrixSet2);
                this.z = wMTSTileMatrixSetLink;
            }
        }
        return this.s;
    }

    private void a(WMTSTileMatrixSet wMTSTileMatrixSet) {
        List<WMTSTileMatrix> tileMatrices = wMTSTileMatrixSet.getTileMatrices();
        int size = tileMatrices == null ? 0 : tileMatrices.size();
        if (size > 1) {
            WMTSTileMatrix wMTSTileMatrix = tileMatrices.get(0);
            for (int i = 1; i < size; i++) {
                WMTSTileMatrix wMTSTileMatrix2 = tileMatrices.get(i);
                if (!wMTSTileMatrix.getTopLeftCorner().equals(wMTSTileMatrix2.getTopLeftCorner()) || wMTSTileMatrix.getTileHeight() != wMTSTileMatrix2.getTileHeight() || wMTSTileMatrix.getTileWidth() != wMTSTileMatrix2.getTileWidth()) {
                    throw new IllegalStateException("Unsupported WMTS tile matrix");
                }
            }
        }
    }

    private void f() {
        List<WMTSTileMatrix> tileMatrices;
        WMTSTileMatrixSet e2 = e();
        if (e2 == null || (tileMatrices = e2.getTileMatrices()) == null) {
            return;
        }
        int size = tileMatrices.size();
        double[] dArr = new double[size];
        double[] dArr2 = new double[size];
        int i = 0;
        for (WMTSTileMatrix wMTSTileMatrix : tileMatrices) {
            dArr[i] = wMTSTileMatrix.getScaleDenominator();
            dArr2[i] = a(dArr[i], getDefaultSpatialReference());
            i++;
        }
        WMTSTileMatrix wMTSTileMatrix2 = tileMatrices.get(0);
        setTileInfo(new TiledServiceLayer.TileInfo(wMTSTileMatrix2.getTopLeftCorner(), dArr, dArr2, i, 90, wMTSTileMatrix2.getTileWidth(), wMTSTileMatrix2.getTileHeight()));
    }

    private static double a(double d2, SpatialReference spatialReference) {
        return d2 / a(spatialReference, 1.0d, (double) d);
    }

    private static double a(SpatialReference spatialReference, double d2, double d3) {
        Unit unit;
        Unit.UnitType unitType;
        if (spatialReference == null || !((unitType = (unit = spatialReference.getUnit()).getUnitType()) == Unit.UnitType.ANGULAR || unitType == Unit.UnitType.LINEAR)) {
            return Double.NaN;
        }
        double d4 = e;
        if (unitType == Unit.UnitType.ANGULAR) {
            d4 = Unit.convertUnits(1.0d, unit, Unit.create(AngularUnit.Code.DEGREE));
        } else if (unitType == Unit.UnitType.LINEAR) {
            d4 = ((LinearUnit) unit).convertToMeters(1.0d);
        }
        return d2 * d4 * c * d3;
    }
}
